package com.xxsc.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2020/12/29 14:20
 */


/**
 * 核心原理
 * 1.导入RedisHttpSessionConfiguration配置
 * 2.给容器中添加组件
 * RedisOperationsSessionRepository 使用redis 操作session
 * SessionRepositoryFilter Filter session 存储过滤器。每个请求过来都需要经过filter
 * filter 核心功能：request.setAttribute
 * request  => SessionRepositoryRequestWrapper
 * response => SessionRepositoryResponseWrapper
 * 接着对原生的请求进行包装成 wrappedRequest wrappedResponse
 * 以后获取session.request.getSession();
 * wrappedRequest.getSession();===>SessionRepository 中获取。就在redis中获取
 * 装饰者模式！！！！！！！
 * 然后在返回！！！！
 * <p>
 * 有自动延期：redis 数据是有过期时间的
 */
@Configuration
public class XxscSessionConfig {
	@Bean
	public CookieSerializer cookieSerializer() {
		DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
		cookieSerializer.setDomainName("xxsc.com");
		cookieSerializer.setCookieName("XXSCSESSION");
		return cookieSerializer;
	}

	@Bean
	public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
		return new GenericJackson2JsonRedisSerializer();
	}
}
